//
//  ViewController.m
//  Topit
//
//  Created by dllo on 15/11/27.
//  Copyright © 2015年 YXY. All rights reserved.
//

#import "ViewController.h"
#import "HomeModel.h"
#import "HomeCollectionViewCell.h"
#import "YXYStrollModel.h"
#import "YXYStrollCollectionViewCell.h"
#import "YXYNextViewController.h"
#import "YXYSkipViewController.h"
#import "DataBaseHandle.h"
#import "YXYAttentionViewController.h"
@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, NextButtonDelegate, skipDidDelagete, nineSkipDelegate, strollDelegate>
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSArray *buttonArray;
@property (nonatomic, retain) NSMutableArray *dataSource;
@property (nonatomic, retain) NSMutableArray *baseArray;

@end

@implementation ViewController
-(void)dealloc {
    [_baseArray release];
    [_collectionView release];
    [_buttonArray release];
    [_dataSource release];
    
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    scrollView.delegate = self;
    self.buttonArray = @[@"精选", @"逛逛"];
    scrollView.contentSize = CGSizeMake(self.view.frame.size.width, 0);
    scrollView.scrollEnabled = NO;
    for (int i = 0; i < _buttonArray.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.tintColor = [UIColor blackColor];
        button.frame = CGRectMake(i * 50, 0, 50, 44);
        [button setTitle:_buttonArray[i] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(pressDidButton:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 100 + i;
        [scrollView addSubview:button];
        if (i == 0) {
            [button setTintColor:[UIColor redColor]];
        }
    }
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"navbarattention"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(pushAttention:)];
    
    [self.view addSubview:self.collectionView];
    self.navigationItem.titleView = scrollView;
    [scrollView release];

    // Do any additional setup after loading the view, typically from a nib.
}



-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.view nightMode];
    self.navigationController.navigationBar.translucent = YES;
    [self.collectionView nightMode];
    [self.collectionView reloadData];
}

- (void)pushAttention:(UIButton *)sender {
    YXYAttentionViewController *attentionVC = [[YXYAttentionViewController alloc] init];
    [self.navigationController pushViewController:attentionVC animated:YES];
    [attentionVC release];
}
#pragma mark - CollectionView 
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLayout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height - 20);
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height) collectionViewLayout:flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.pagingEnabled = YES;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:[HomeCollectionViewCell class] forCellWithReuseIdentifier:@"HomeCollectionViewCellIdentifier"];
        [_collectionView registerClass:[YXYStrollCollectionViewCell class] forCellWithReuseIdentifier:@"YXYStrollCollectionViewCellIdentifier"];
        [_collectionView reloadData];
        [flowLayout release];
        [_collectionView release];
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    return _collectionView;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 2;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.item == 0) {
        HomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeCollectionViewCellIdentifier" forIndexPath:indexPath];
        //跳转代理
        [cell.tableView reloadData];
        cell.delegate = self;
        cell.skipDelegate = self;
        cell.nineSkipDelegate = self;
        return cell;
    }else {
        YXYStrollCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YXYStrollCollectionViewCellIdentifier" forIndexPath:indexPath];
        cell.strollDelegate = self;
        return cell;
    }
    
}



#pragma mark - 导航栏
- (void)pressDidButton:(UIButton *)sender {
    UIButton *button = (UIButton *)[self.navigationItem.titleView viewWithTag:sender.tag];
    button.tintColor = [UIColor redColor];
    
    [self.collectionView setContentOffset:CGPointMake((sender.tag - 100) * self.view.frame.size.width, 0) animated:YES];
    for (int i = 0; i < _buttonArray.count; i++) {
        if (sender.tag == 100 + i) {
            continue;
        }else {
            UIButton *button = (UIButton *)[self.navigationItem.titleView viewWithTag:100 + i];
            [button setTintColor:[UIColor blackColor]];
        }
    }

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    UIButton *button = (UIButton *)[self.navigationItem.titleView viewWithTag:100 + scrollView.contentOffset.x / self.view.frame.size.width];
    [button setTintColor:[UIColor redColor]];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    for (int i = 0; i < _buttonArray.count; i++) {
        UIButton *button = (UIButton *)[self.navigationItem.titleView viewWithTag:100 + i];
        [button setTintColor:[UIColor blackColor]];
    }
}

#pragma mark - 跳转
-(void)nextDidPressWithURl:(NSString *)url {
    YXYNextViewController *nextViewController = [[YXYNextViewController alloc] initWithNextUrl:url];
    nextViewController.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController pushViewController:nextViewController animated:YES];
    [nextViewController release];
}

#pragma mark - 精选界面跳转
- (void)selectionDidPressWithURL:(NSString *)url {
    NSRange range = [url rangeOfString:@"album"];
    if (range.length) {
        YXYSelectionViewController *selectionVC = [[YXYSelectionViewController alloc] initWithURL:url];
        selectionVC.view.backgroundColor = [UIColor whiteColor];
        [self.navigationController pushViewController:selectionVC animated:YES];
        [selectionVC release];
    }else {
        [ProgressHUD showError:@"这是广告"];
    }
}

#pragma mark - zoomImage跳转
- (void)skipDidPress:(NSString *)url {
    YXYSkipViewController *skipVC = [[YXYSkipViewController alloc] initWithURL:url];
    skipVC.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController pushViewController:skipVC animated:YES];
    [skipVC release];
}

- (void)nineImagePressSkipWithURL:(NSString *)url {
    YXYSkipViewController *skipVC = [[YXYSkipViewController alloc] initWithURL:url];
    skipVC.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController pushViewController:skipVC animated:YES];
    [skipVC release];
}

- (void)strollDelegateWithURl:(NSString *)url {
    YXYSkipViewController *skipVC = [[YXYSkipViewController alloc] initWithURL:url];
    skipVC.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController pushViewController:skipVC animated:YES];
    [skipVC release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
